﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml;

namespace IntellectShop.Common
{
	public partial class Software : Commodity
	{
		[ScaffoldColumn(false)]
		public int SoftwareID { get; set; }

		[DisplayNameLocalized("Category")]
		[Required(ErrorMessage = "Software category is required")]
		public Common.Category Category { get; set; }

		public int CategoryID
		{
			get { return this.Category.CategoryID; }
			set { this.Category.CategoryID = value; }
		}

		[ScaffoldColumn(false)]
		public override CommodityType CommodityType
		{
			get
			{
				return Common.CommodityType.Software;
			}
		}

		public Software()
		{
			this.Category = new Common.Category();
		}

		public override string ToString()
		{
			return String.Format("{0}", this.Name);
		}

		#region Import/Export

		public static XmlDocument ExportToXml()
		{
			XmlDocument doc = new XmlDocument();
			XmlElement root = doc.CreateElement("Softwares");
			doc.AppendChild(root);

			Dictionary<Common.Category, List<Common.Software>> softwaresByCategory = IntellectShop.Common.Software.RetrieveAll();
			foreach (var kvp in softwaresByCategory)
			{
				foreach (var soft in kvp.Value)
				{
					XmlElement courseElement = doc.CreateElement("Software");
					root.AppendChild(courseElement);

					courseElement.SetAttribute("CommodityID", soft.CommodityID.ToString());
					courseElement.SetAttribute("CategoryName", soft.Category.Name);
					courseElement.SetAttribute("Code", soft.Code);
					courseElement.SetAttribute("Name", soft.Name);
					courseElement.SetAttribute("Price", soft.Price.Amount.ToString());
					courseElement.SetAttribute("Tags", soft.TagsString);
					courseElement.SetAttribute("Description", soft.Description);

					if (soft.Created.HasValue)
					{
						courseElement.SetAttribute("Created", soft.Created.Value.ToString("o"));
						courseElement.SetAttribute("CreatedBy", soft.CreatedBy);
					}

					if (soft.Updated.HasValue)
					{
						courseElement.SetAttribute("Updated", soft.Updated.Value.ToString("o"));
						courseElement.SetAttribute("UpdatedBy", soft.UpdatedBy);
					}
				}
			}

			return doc;
		}

		public static void ImportFromXml(XmlDocument doc, string actionBy)
		{
			if (doc == null)
			{
				throw new ArgumentNullException("Software.ImportFromXml, doc == null");
			}

			XmlNode empty = doc.CreateElement("Empty");
			XmlNodeList coursesElements = doc.SelectNodes("//Software");
			foreach (XmlNode node in coursesElements)
			{
				string code = (node.SelectSingleNode("@Code") ?? empty).Value;
				if (String.IsNullOrEmpty(code))
				{
					throw new ArgumentNullException("Не задан код товара.");
				}

				Commodity com = Commodity.RetrieveByCode(code);
				Software soft;
				if (com == null)
				{
					soft = new Software();
					soft.Code = code;
				}
				else
				{
					soft = Software.RetrieveByCommodityID(com.CommodityID);
				}

				string categoryName = (node.SelectSingleNode("@CategoryName") ?? empty).Value;
				if (String.IsNullOrEmpty(categoryName))
				{
					throw new ArgumentNullException("Не задана категория товара.");
				}

				Category category = Category.RetrieveByName(categoryName);
				if (category == null)
				{
					category = new Category();
					category.Name = categoryName;
					category.Create(actionBy);
				}

				soft.Category = category;
				soft.CategoryID = category.CategoryID;

				if (node.SelectSingleNode("@Name") != null)
				{
					soft.Name = node.SelectSingleNode("@Name").Value;
				}

				decimal amount;
				if (Decimal.TryParse((node.SelectSingleNode("@Price") ?? empty).Value, out amount))
				{
					soft.Price = new Common.Money { Amount = amount, Currency = Currency.RUB };
				}

				if (node.SelectSingleNode("@Tags") != null)
				{
					soft.TagsString = node.SelectSingleNode("@Tags").Value;
				}

				if (node.SelectSingleNode("@Description") != null)
				{
					soft.Description = node.SelectSingleNode("@Description").Value;
				}

				if (soft.CommodityID == 0)
				{
					soft.Create(actionBy);
				}
				else
				{
					soft.Update(actionBy);
				}
			}
		}

		#endregion

	}
}
